package com.itheima.thread2;

public class Demo1Jmm {

    public static void main(String[] args) throws InterruptedException {

        JmmDemo demo = new JmmDemo();
        Thread t = new Thread(demo);
        t.start();
        Thread.sleep(100);
        //主线程修改flag的时候,会把最新的值,刷新到主内存中
        demo.flag = false;
        System.out.println("已经修改为false");
        System.out.println(demo.flag);
    }

    static class JmmDemo implements Runnable {
        //public boolean flag = true;
        public  boolean flag = true;

        public void run() {
            System.out.println("子线程执行。。。");
            while (flag) {
                //同步代码块释放锁的时候,会把所有最新的遍历值刷新到主内存中

                //同步代码块在获取锁的时候,会清空线程本地内存,从主内从中获取新的数据
                synchronized (this) {
                }
            }
            System.out.println("子线程结束。。。");
        }
    }
}